package com.zxd.controller;

import com.zxd.controller.common.CommonController;
import com.zxd.core.Constant;
import com.zxd.entity.Master;
import com.zxd.entity.Module;
import com.zxd.entity.RoleModule;
import com.zxd.service.MasterService;
import com.zxd.service.RoleModuleService;
import com.zxd.utils.Md5Util;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 初始化控制层
 * Created by 涂奕恒 on 2016/10/18.
 */
@Controller
@RequestMapping(value = "")
public class IndexController extends CommonController {

    @Autowired
    private MasterService masterService;

    @Autowired
    private RoleModuleService roleModuleService;

    @RequestMapping(value = "/login")
    public String login(HttpServletRequest request, String error, ModelMap model) {
        masterService.loginOut(request);

        if (StringUtils.isNotBlank(error)) {
            model.addAttribute("error", error);
        }
        return "login";
    }

    @RequestMapping(value = "/login/check")
    public String checkLogin(HttpServletRequest request,
                             String account,
                             String password,
                             ModelMap model) {
        try {
            Master master = masterService.loginIn(request, account, password);
            if (null != master) {
                // 查询该管理员的所有菜单信息
                List<Module> moduleList = roleModuleService.findListByMasterId(master.getId());
                request.getSession().setAttribute("moduleList", moduleList);

                // 校验登录人是否有上传文件和删除文件的权限，0=没有，1=有
                int allowUpload = 0;
                int allowDelete = 0;

                for (RoleModule roleModule : roleModuleService.paramsByMasterId(master.getId())) {
                    if (roleModule.getModule().getId().equals(4)) {
                        allowUpload = 1;
                    }

                    if (roleModule.getModule().getId().equals(5)) {
                        allowDelete = 1;
                    }
                }

                request.getSession().setAttribute("allowUpload", allowUpload);
                request.getSession().setAttribute("allowDelete", allowDelete);

                return "redirect:/backend/dashboard";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        model.addAttribute("error", "用户名或密码错误!");
        return "redirect:/login";
    }

    @RequestMapping(value = "/logout")
    public String logout(HttpServletRequest request) {
        masterService.loginOut(request);
        return "login";
    }

    @RequestMapping(value = "/")
    public String index() {
        return "redirect:/backend/dashboard";
    }

    @RequestMapping(value = "/changePwd")
    public String changePwd() {
        return "backend/modifyPwd";
    }

    /**
     * 检测当前登录人旧密码是否正确
     */
    @RequestMapping("/check/oldPwd")
    @ResponseBody
    public Integer checkOldPwd(HttpServletRequest request, String oldPwd) {
        Master master = (Master) request.getSession().getAttribute(Constant.CURRENT_LOGIN_MASTER);
        if (!master.getPassword().equals(Md5Util.md5(oldPwd))) {
            return 0;
        } else {
            return 1;
        }
    }

    @RequestMapping("/modifyPwd")
    @ResponseBody
    public Integer modifyPwd(HttpServletRequest request, String newPwd) {
        return masterService.modifyPwd(request, newPwd);
    }
}